﻿using Collection.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace 自定义集合
{
    internal class Program
    {
        static void Main(string[] args)
        {
            



            ReadKey();
        }

        public static void Test2()
        {
            Animals animals = new Animals 
            { 
            
            
            };
        }



        public static void Test1()
        {
            Animals animals = new Animals();
            animals.Add(new Cow("Jim"));
            animals.Add(new Chicken("Tim"));

            Cow c = new Cow("AAA");

            animals = animals + c;

            foreach (var item in animals)
            {
                Type type = item.GetType();
                if (item.GetType() == typeof(Cow))
                {
                    ((Cow)item).Milk();
                }
                else if (item.GetType() == typeof(Chicken))
                {
                    (item as Chicken).LayEgg();
                }
            }

            // 类型比较
            foreach (var item in animals)
            {
                if (item is Cow)
                {
                    ((Cow)item).Milk();
                }
                else if (item is Chicken)
                {
                    //((Chicken)item).LayEgg();
                    (item as Chicken).LayEgg();
                }
            }
        }
    }

    class Animals : CollectionBase
    {
        public void Add(Animal animal) => List.Add(animal);
        public void Remove(Animal animal) => List.Remove(animal);
        public Animals() { }

        public Animal this[int index]
        {
            get
            {
                return (Animal)List[index];
            }
            set
            {
                List[index] = value;
            }
        }
        public void CopyTo(Animals animals)
        {
            for(int index = 0;index < this.Count;index++)
            {
                animals[index] = this[index];
            }
        }

        /// <summary>
        /// 运算符重载
        /// </summary>
        /// <param name="animals">动物列表</param>
        /// <param name="animal">一个动物</param>
        /// <returns></returns>
        public static Animals operator + (Animals animals,Animal animal)
        {
            animals.Add(animal);
            return animals;
        }

        public static Animals operator -(Animals animals, Animal animal)
        {
            if(animals != null && animals.Count > 0)
            {
                animals.Remove(animal);
            }
            return animals;
        }

        public static Animals operator + (Animals a1,Animals a2)
        {
            if(a1 != null && a2 != null)
            {
                foreach (Animal item in a2)
                {
                    a1.Add(item);
                }
            }
            return a1;
        }

    }
}
